package com.zhkj.lib.utils

import android.content.Context
import android.content.SharedPreferences
import com.zhkj.lib.base.BaseApplication

object SPUtil {


    fun getInstance(name: String = "token"): SP {
        return SP(name)
    }

    class SP(name: String = "token") {
        private var sp: SharedPreferences =
            BaseApplication.INSTANCE.getSharedPreferences(name, Context.MODE_PRIVATE)

        fun putString(key: String, value: String): SP {
            sp.edit()?.putString(key, value)?.apply()
            return this
        }

        fun putBoolean(key: String, value: Boolean): SP {
            sp.edit()?.putBoolean(key, value)?.apply()
            return this
        }

        fun putInt(key: String, value: Int): SP {
            sp.edit()?.putInt(key, value)?.apply()
            return this
        }

        fun putLong(key: String, value: Long): SP {
            sp.edit()?.putLong(key, value)?.apply()
            return this
        }

        fun clear() {
            sp.edit().clear().apply()
        }

        fun getString(key: String): String? {
            return sp.getString(key, null)
        }

        fun getBoolean(key: String, default: Boolean): Boolean {
            return sp.getBoolean(key, default)
        }

        fun getInt(key: String, default: Int): Int {
            return sp.getInt(key, default)
        }

        fun getLong(key: String, default: Long): Long {
            return sp.getLong(key, default)
        }
    }
}